package com.correo.vista.server.windows.ventanasInternas;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JDialog;

import com.correo.controlador.server.beans.UserDBBean;
import com.correo.database.beans.IBean;
import com.correo.database.beans.UserBean;
import com.correo.exceptions.server.RefreshException;
import com.correo.vista.server.windows.AdministracionCorreo;
import com.correo.vista.server.windows.componentes.eventos.ventanasInternas.ActionWindowsUser;

public class DialogUser extends JDialog{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private UserBean			userBean;
	private boolean				buttonOk;
	private AdministracionCorreo adminCorreo;
	/** Creates new form AddObject */
    public DialogUser(java.awt.Frame parent,AdministracionCorreo adminCorreo, boolean modal) {
        super(parent, modal);
        createComponents();
        initComponents();
        this.adminCorreo = adminCorreo;
        //this.model = model;
    }

    private void createComponents() {
        labelUser = new javax.swing.JLabel();
        textFieldUser = new javax.swing.JTextField();
        labelPassword = new javax.swing.JLabel();
        buttonAceptar = new javax.swing.JButton();
        buttonCancelar = new javax.swing.JButton();
        passwordField = new javax.swing.JPasswordField();
        userBean = new UserBean();
        buttonOk=false;
	}


    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {


        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        setTitle("Alta");

        labelUser.setText("Usuario :");        
        textFieldUser.setText("");        
        labelPassword.setText("Password :");        
        buttonAceptar.setText("Aceptar");        
        buttonCancelar.setText("Cancelar");  
        passwordField.setText("");

        setActionListener();
        //buttonAceptar.addActionListener(new ActionListenerAceptarAltaUserDB(model,AdministracionCorreo));
        setLayoutDialog();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-231)/2, (screenSize.height-154)/2, 231, 154);
        setResizable(false);
        //pack();
    }// </editor-fold>
	private void setActionListener() {
		
		//cirra la ventana
		buttonCancelar.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				textFieldUser.setText("");
				passwordField.setText("");
				buttonOk=false;
				setVisible(false);		
				
				
			}
			
		});
		buttonAceptar.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				buttonOk=true;
				setVisible(false);		
				
				userBean.setId(textFieldUser.getText());
				userBean.setPasword(new String(passwordField.getPassword()));
				textFieldUser.setText("");
				passwordField.setText("");
				
			}
			
		});
		this.addWindowListener(new ActionWindowsUser(this));
		
	}

	private void setLayoutDialog() {
	       final javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
	        getContentPane().setLayout(layout);
	        layout.setHorizontalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                    .addGroup(layout.createSequentialGroup()
	                        .addContainerGap()
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	                            .addComponent(labelUser)
	                            .addComponent(labelPassword))
	                        .addGap(14, 14, 14)
	                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
	                            .addComponent(passwordField)
	                            .addComponent(textFieldUser, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)))
	                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
	                        .addContainerGap()
	                        .addComponent(buttonAceptar)
	                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                        .addComponent(buttonCancelar)))
	                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	        );
	        layout.setVerticalGroup(
	            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
	            .addGroup(layout.createSequentialGroup()
	                .addContainerGap()
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(labelUser)
	                    .addComponent(textFieldUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(labelPassword)
	                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
	                .addGap(27, 27, 27)
	                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
	                    .addComponent(buttonCancelar)
	                    .addComponent(buttonAceptar))
	                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
	        );
		
	}

	public javax.swing.JButton getButtonAceptar() {
		return buttonAceptar;
	}

	
    // Variables declaration - do not modify
    private javax.swing.JButton 		buttonAceptar;
    private javax.swing.JButton 		buttonCancelar;
    private javax.swing.JLabel 			labelUser;
    private javax.swing.JLabel 			labelPassword;
    private javax.swing.JPasswordField 	passwordField;
    private javax.swing.JTextField 		textFieldUser;
    
    //private ControllerUserDB 			controlador;
    // End of variables declaration
    
   // private ModelServer 				model;
//    private UserDBBean					userDBBean;

    
    
//    public UserDBBean getUserDBBean() {
//    	if(userDBBean==null){
//    		userDBBean = new UserDBBean(textFieldUser.getText(),new String(passwordField.getPassword()));
//    	}else{
//    		userDBBean.setUser(textFieldUser.getText());
//    		userDBBean.setPassword(new String(passwordField.getPassword()));
//    	}
//    	//this.setVisible(false);
//		return userDBBean;
//	}
//
//	public void setUserDBBean(UserDBBean userDBBean) {
//		textFieldUser.setText(userDBBean.getUser());
//		passwordField.setText(userDBBean.getPassword());
//		this.userDBBean = userDBBean;
//	}

	public UserBean getUserBean() {
		return userBean;
	}

	public boolean isButtonOk() {
		return buttonOk;
	}

	public void setUserBean(IBean userBean) {
		textFieldUser.setText(((UserDBBean)userBean).getUser());
		passwordField.setText(((UserDBBean)userBean).getPassword());
		this.userBean = ((UserBean)userBean);
	}

	public void setData(UserBean user) {
		userBean = user;
		if(user!=null){
			textFieldUser.setText(user.getId());
			passwordField.setText(user.getPasword());
		}
	}

	public boolean isBotonOk() {
		// TODO Auto-generated method stub
		return buttonOk;
	}

	public IBean getData() {
		// TODO Auto-generated method stub
		return userBean;
	}

	public void setActualizarTabla(List<IBean> refresh) throws RefreshException, SQLException, ClassNotFoundException {
		this.adminCorreo.actualizarTabla(refresh);
		
	}

	public void setEditable(boolean modify, boolean view) {
		textFieldUser.setEditable(true);
		passwordField.setEditable(true);
		if(modify){
			textFieldUser.setEditable(false);
			this.setTitle("Modificar Usuario "+textFieldUser.getText());
		}
		if(view){
			this.setTitle("Ver Usuario "+textFieldUser.getText());
			textFieldUser.setEditable(false);
			passwordField.setEditable(false);			
		}
		if(!modify && !view){
			this.setTitle("Nuevo Usuario ");
			
			textFieldUser.setText("");
			passwordField.setText("");
		}
		
	}

}
